﻿// ////////////////////////////////////////////////////////////////////////////
// 
// SWE Case Tool
// 
// Solution:	CaseTool
// Project:		4. MethodCocomo
// Filename:	CtrlCocomoFactorGroup.cs
// 
// File Created:	17:57 31.05.2013 
// Last Changed:	18:16 05.06.2013
// 
// ////////////////////////////////////////////////////////////////////////////

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace SWE.CaseTool.Methods.Controls
{
	/// <summary>
	/// Holds a group of controls. Adds each new control below the others and calls the IChanged event of it's parent if possible.
	/// </summary>
	public class CtrlCocomoFactorGroup : GroupBox, IChanged
	{
		private readonly Panel container = new ChangedPanel
			{
				Dock = DockStyle.Fill
			};

		/// <summary>
		/// The the y position after the last added control
		/// </summary>
		private int _yPosition;

		/// <summary>
		/// Initializes a new instance of the <see cref="CtrlCocomoFactorGroup"/> class.
		/// </summary>
		public CtrlCocomoFactorGroup()
		{
			DoubleBuffered = true;
			Changed += (sender, args) =>
				{
					if (Parent is IChanged && ((IChanged) Parent).Changed != null)
					{
						((IChanged) Parent).Changed(this, EventArgs.Empty);
					}
				};

			Controls.Add(container);
		}

		/// <summary>
		/// Gets or sets a value indicating whether the widghets content [auto scroll].
		/// </summary>
		/// <value>
		///   <c>true</c> if [auto scroll]; otherwise, <c>false</c>.
		/// </value>
		public bool AutoScroll
		{
			get { return container.AutoScroll; }
			set { container.AutoScroll = value; }
		}

		/// <summary>
		/// Adds the control.
		/// </summary>
		/// <param name="ctrl">The CTRL.</param>
		public void AddControl(Control ctrl)
		{
			ctrl.Location = new Point(0, _yPosition);
			ctrl.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
			ctrl.Width = container.ClientSize.Width;
			ctrl.AutoSize = false;
			container.Controls.Add(ctrl);

			_yPosition += ctrl.Size.Height;

			if (!AutoScroll)
			{
				Height = _yPosition + 20;
			}
		}

		/// <summary>
		/// Gets or sets the changed event handler for notifiing the parent of changes in it's child objects.
		/// E.G. a change in a
		/// subcontrol which modifies a parameter, a recalcutation of the whole project is needed. The child
		/// control then uses the Changed-Method of it's parent to tell him that something has changed
		/// </summary>
		/// <value>
		/// The changed event handler.
		/// </value>
		[Browsable(false)]
		[EditorBrowsable(EditorBrowsableState.Never)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public ChangedEventHandler Changed { get; set; }
	}
}